package xyz.lne.researchgroupmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.lne.researchgroupmanage.entity.SysUser;
import xyz.lne.researchgroupmanage.exception.BadRequestException;
import xyz.lne.researchgroupmanage.mapper.SysUserMapper;
import xyz.lne.researchgroupmanage.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.lne.researchgroupmanage.service.dataTransferObject.UserDTO;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<UserDTO> listAll() {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.isNotNull("user_id");
        List<SysUser> sysUserList=sysUserMapper.selectList(wrapper);

        List<UserDTO> userDTOList=new ArrayList<>();
        for (SysUser user: sysUserList) {
            userDTOList.add(sysUser2UserDTO(user));
        }
        return userDTOList;
    }


    @Override
    public String findNameById(Integer userId) {
        SysUser sysUser=sysUserMapper.selectById(userId);
        if (sysUser==null) {
            return null;
        } else {
            return sysUser.getName();
        }

    }


    @Override
    public UserDTO findUserById(Integer userId) {
        isLegal(userId);
        SysUser sysUser=sysUserMapper.selectById(userId);

        return sysUser2UserDTO(sysUser);
    }

    @Override
    public UserDTO addUser(UserDTO userDTO) {
        SysUser sysUser=userDTO2SysUser(userDTO);

        if(sysUser==null) {
            Map<String, Object> e = new HashMap<>();
            e.put("输入为空", null);
            throw new BadRequestException(e);
        }

        //用户账号不可重复
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("account", userDTO.getAccount());
        if(sysUserMapper.selectCount(wrapper)>=1) {
            Map<String, Object> e = new HashMap<>();
            e.put("账户名不可重复", userDTO.getAccount());
            throw new BadRequestException(e);
        }

        sysUserMapper.insert(sysUser);

        return findUserById(sysUser.getUserId());
    }

    @Override
    public UserDTO updateUser(UserDTO userDTO) {
        if(userDTO==null) {
            return null;
        }

        SysUser sysUser=userDTO2SysUser(userDTO);
        sysUserMapper.updateById(sysUser);
        return findUserById(sysUser.getUserId());
    }

    @Override
    public UserDTO findUserByAccount(String account) {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("account",account);
        if(sysUserMapper.selectCount(wrapper)==0){
            Map<String, Object> e = new HashMap<>();
            e.put("用户不存在", account);
            throw new BadRequestException(e);
        }
        SysUser sysUser=sysUserMapper.selectOne(wrapper);
        return sysUser2UserDTO(sysUser);
    }

    @Override
    public Boolean isAccountAvailable(String account) {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("account",account);
        if(sysUserMapper.selectCount(wrapper)==0){
            return true;
        }
        return false;
    }

    @Override
    public void isLegal(Integer userId) {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);

        if(sysUserMapper.selectCount(wrapper)==0){
            Map<String, Object> e = new HashMap<>();
            e.put("请求中包含不存在的用户", userId);
            throw new BadRequestException(e);
        }
    }

    //TODO：need mapstruct conversion
    /**
     * Convert UserDTO to SysUser entity
     * Note that the properties of SysUser is precisely equals to the UserDTO
     * @param sysUser the SysUser entity need to be converted
     * @return UserDTO
     */
    private UserDTO sysUser2UserDTO(SysUser sysUser){
        if (sysUser==null) {
            return null;
        }

        UserDTO userDTO=new UserDTO();
        userDTO.setUserId(sysUser.getUserId());
        userDTO.setAccount(sysUser.getAccount());
        userDTO.setLevel(sysUser.getLevel());
        userDTO.setPassword(sysUser.getPassword());
        userDTO.setEmail(sysUser.getEmail());
        userDTO.setName(sysUser.getName());
        userDTO.setPhoneNumber(sysUser.getPhoneNumber());
        userDTO.setSex(sysUser.getSex());
        userDTO.setBirthday(sysUser.getBirthday());
        userDTO.setCompany(sysUser.getCompany());
        userDTO.setPosition(sysUser.getPosition());
        userDTO.setIntroduction(sysUser.getIntroduction());
        userDTO.setUserImg(sysUser.getUserImg());
        userDTO.setCreateDate(sysUser.getCreateDate());
        userDTO.setIsDeleted(sysUser.getIsDeleted());

        return userDTO;
    }

    /**
     * Convert UserDTO to SysUser entity
     * Note that the userDTO.userId could not be null.
     * For details.   SysUserController.updateUser
     * @param userDTO
     * @return
     */
    private SysUser userDTO2SysUser(UserDTO userDTO){
        if(userDTO==null) {
            return null;
        }

        SysUser sysUser =new SysUser();

        sysUser.setUserId(userDTO.getUserId());
        sysUser.setAccount(userDTO.getAccount());
        sysUser.setLevel(userDTO.getLevel());
        sysUser.setPassword(userDTO.getPassword());
        sysUser.setEmail(userDTO.getEmail());
        sysUser.setName(userDTO.getName());
        sysUser.setPhoneNumber(userDTO.getPhoneNumber());
        sysUser.setSex(userDTO.getSex());
        sysUser.setBirthday(userDTO.getBirthday());
        sysUser.setCompany(userDTO.getCompany());
        sysUser.setPosition(userDTO.getPosition());
        sysUser.setIntroduction(userDTO.getIntroduction());
        sysUser.setUserImg(userDTO.getUserImg());
        sysUser.setCreateDate(userDTO.getCreateDate());
        sysUser.setIsDeleted(userDTO.getIsDeleted());

        return sysUser;
    }
}
